<!-- 用户名密码登录 -->
<template>
	<view class="loginPage">
		<view class="loginPage-title">
			赤兔养车
		</view>
		<view class="loginPage-hello">
			登录之后更精彩
		</view>
		<u-toast ref="showWronglogin" />
		<!-- 电话号码和验证码 -->
		<view class="confirmedtele">
			<view class="tele">
				<input placeholder="请输入手机号/账号" @blur="checktelephone" v-model="account" class="tele-ipt" type="text" />
			</view>
			<view class="password">
				<input placeholder="请输入密码" v-model="password" class="tele-ipt" type="text" />
			</view>
		</view>

		<view class="loginbutton" @tap="loginBypsw">
			登录
		</view>

		<view class="loginmethods">
			<navigator url="/pages/loginlrx/loginlrx"><text class="methoditem">验证码登录</text></navigator>

			<navigator url="/pages/register/register"><text class="methoditem">立即注册</text></navigator>

		</view>
	</view>

</template>

<script>
	import {
		createNamespacedHelpers
	} from "vuex";
	let {
		mapActions,
		mapMutations
	} = createNamespacedHelpers("users");
	export default {
		data() {
			return {
				border: true,
				account: '',
				password: '',
			}
		},
		methods: {
			...mapActions(["loginbyPassword"]),
			...mapMutations(["setUserInfo"]),
			async loginBypsw() {
				if (this.account && this.password) {
					// 如果有账号密码
					let data = await this.loginbyPassword({
						account: this.account,
						password: this.password
					})
					if (data.token) {
						//登录成功，保存token到本地
						uni.setStorageSync("token", data.token);
						uni.setStorageSync("id", data.custom.id); //保存用户id到本地
						this.setUserInfo(data.custom);
						uni.switchTab({
							url: '/pages/mine/mine'
						});
					} else {
						this.$refs.showWronglogin.show({
							title: '账号名或密码错误',
							type: 'error',
							position: "top"
						})
					}

				} else {
					this.$refs.showWronglogin.show({
						title: '请输入正确的账号密码',
						type: 'error',
						position: "top"
					})
				}
			},
			//失焦判断电话号码是否正确
			checktelephone() {
				let result = RegExp(/^1[34578]\d{9}$/).test(this.account);
			}
		},
	}
</script>
<style scoped lang="scss">
	.loginPage {
		padding-bottom: 300rpx;
		background-color: white;

		.loginPage-title {
			margin: 0rpx auto;
			text-align: center;
			font-size: 46rpx;
			color: #f5610c;
			font-weight: bolder;
			line-height: 200rpx;
		}

		.loginPage-hello {
			font-size: 50rpx;
			padding: 0rpx 50rpx;
			color: #f5610c;
			font-weight: 400;
		}


		// 验证码和手机号
		.confirmedtele {
			width: 650rpx;
			margin: 40rpx auto;

			// 电话号码
			.tele {
				border-radius: 100rpx;
				height: 80rpx;
				background-color: #f2f2f2;
				margin-top: 150rpx;
				display: flex;


				.tele-ipt {
					line-height: 80rpx;
					width: 585rpx;
					height: 80rpx;
					margin-left: 32rpx;
				}
			}

			.password {
				border-radius: 100rpx;
				height: 80rpx;
				background-color: #f2f2f2;
				margin-top: 32rpx;
				display: flex;


				.tele-ipt {
					line-height: 80rpx;
					width: 585rpx;
					height: 80rpx;
					margin-left: 32rpx;
				}
			}
		}

		// 登录按钮
		.loginbutton {
			width: 650rpx;
			margin: 100rpx auto;
			margin-bottom: 30rpx;
			text-align: center;
			line-height: 80rpx;
			color: white;
			border-radius: 100rpx;
			background-color: #f5610c;
		}

		.loginmethods {
			display: flex;
			padding: 10rpx 60rpx;
			justify-content: space-between;

			.methoditem {
				color: #f5610c;
			}
		}

	}
</style>
